package com.fg.cloud.manager.service.agency.impl;

import java.io.IOException;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyUserMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.agency.AgencyUserService;

/**
 * 经销商信息service实现
 * @author around
 * @date 2017-6-21
 */
@Service
public class AgencyUserServiceImpl extends BaseServiceImpl implements AgencyUserService {

	@Autowired
	private AgencyUserMapper agencyUserMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return agencyUserMapper;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T extends BaseEntity> T find(long id) {
		return (T) agencyUserMapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional
	public boolean deletes(String key, String values) throws IOException {
		return agencyUserMapper.deletes(key, values.split("[, ]+")) > 0 ? true : false;
	}

	@Override
	public void findList(PageUtils<AgencyUserVo> pageinfo) {
		long total = agencyUserMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if(total > 0) {
			pageinfo.setData(agencyUserMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<AgencyUserVo>());
		}
	}

	@Override
	@Transactional
	public boolean deletes(Long userId, Long facId, Long[] ids) {
		return agencyUserMapper.updateStatus(userId, facId, ids, -1) > 0 ? true : false;
	}

	@Override
	public AgencyUserVo selectUserByUserName(String userName) {
		return agencyUserMapper.selectUserByUserName(userName);
	}

	/**
	 * 重置密码
	 */
	@Override
	@Transactional
	public boolean resetPassword(Long id, String oldPassword, String newPassword) {
		return agencyUserMapper.resetPassword(id, oldPassword, newPassword) > 0 ? true : false;
	}

	@Override
	public boolean verifyUniquely(Long id, String name) {
		return agencyUserMapper.verifyUniquely(id, name) > 0 ? false : true;
	}
	
}
